var tree;
var currentId;

Ext.onReady(function() {
	tree = new Ext.tree.TreePanel({
		region : 'center',
		useArrows : true,
		autoScroll : false,
		animate : true,
		enableDD : true,
		border : false,
		containerScroll : true,
		singleExpand : true,
		rootVisible : false,
		layout : 'fit',
		frame : false,
		root : {
			nodeType : 'async'
		},

		// auto create TreeLoader
		dataUrl : context + '/app/info.do?method=directorytree4manager&id=0',

		listeners : {
			'checkchange' : function(node, checked) {
				if (checked) {
					node.getUI().addClass('complete');
				} else {
					node.getUI().removeClass('complete');
				}
			},
			'click' : function(node) {
				// alert(node.id);
				// alert(node.text);
				// alert(node.attributes.type);
				if (node.attributes.purview == 0) {
					document.getElementById('frm').src = context
							+ '/app/info.do?method=viewdirectory&id=' + node.id;
				} else if (node.attributes.purview == 1) {
					document.getElementById('frm').src = context
							+ '/app/info.do?method=editdirectory&id=' + node.id;
				}
			},
			'beforeload' : function(node) {
				if (currentId && node.id == currentId) {
					tree.getSelectionModel().select(node);
				}
			},
			'nodedrop' : function(e) {
				appendNode(e.dropNode, e.target, e.point);
			}
		}
	});

	tree.getRootNode().expand(true);

	var tabs = new Ext.TabPanel({
				id : 'consoletabs',
				region : 'center', // a center region is ALWAYS
				// required for border layout
				closable : true,
				resizeTabs : true,
				minTabWidth : 150,
				tabWidth : 100,
				border : false,
				deferredRender : false,
				enableTabScroll : true,
				margins : '5 5 5 0',
				activeTab : 0, // first tab initially active
				items : []
			});

	var viewport = new Ext.Viewport({
				layout : 'border',
				items : [{
					region : 'west',
					split : true,
					// animCollapse:false,
					// animate: false,
					// collapseMode : 'mini',
					split : false,
					width : 250,
					minWidth : 200,
					maxWidth : 250,
					layout : 'fit',
					margins : '1 0 1 1',
					items : [tree]
				}, {
					layout : 'fit',
					region : 'center',
					html : '<iframe id="frm" name="frm" src="'
							+ context
							+ '/system/blank.jsp" width="100%" height="100%" frameborder="0"></iframe>',
					border : false,
					margins : '1 2 2 1'
				}]
			});
});

function reloadTree(id) {
	currentId = id;
	tree.root.reload();
	tree.getRootNode().expand(true);
}

function addNode(id, name, iconCls, pid) {
	var node = new Ext.tree.TreeNode({
				id : id,
				text : name,
				isroot : false,
				leaf : false,
				parentId : pid,
				haschildren : false,
				iconCls : iconCls
			});

	tree.getNodeById(pid).appendChild(node);
}

function updateNode(name) {
	tree.getSelectionModel().getSelectedNode().setText(name);
}

function deleteNode() {
	tree.getSelectionModel().getSelectedNode().remove();
}

function appendNode(dropNode, targetNode, tp) {
	var targetIndex = targetNode.parentNode.indexOf(targetNode);
	var dropIndex;

	if (dropNode.attributes.pid == targetNode.attributes.pid) {
		dropIndex = dropNode.parentNode.indexOf(dropNode);
	} else {
		dropIndex = targetNode.childNodes.length;
	}

	Ext.Ajax.request({
				url : context + "/app/info.do?method=savesort",
				method : 'POST',
				async : false,
				params : {
					id : dropNode.id,
					idx : dropIndex,
					pid : dropNode.attributes.pid,
					tid : targetNode.id,
					tidx : targetIndex,
					tpid : (tp == 'append'
							? targetNode.id
							: targetNode.attributes.pid)
				},
				success : function(response, options) {
					var o = Ext.util.JSON.decode(response.responseText);
					if (!o.success) {
						Ext.Msg.alert('提示', '排序调整发生错误，请重试！');
						tree.root.reload();
						tree.getRootNode().expand(true);
					} else {
						// dropNode.iconCls = o.changeIcon;
						// alert(o.changeIcon);
						// var el = dropNode.getUI().getIconEl();
						// el.className = "'x-tree-node-icon " + o.changeIcon;

						var newCls = o.changeIcon;
						var iconEl = dropNode.getUI().rendered ? Ext
								.get(dropNode.getUI().getIconEl()) : null;
						if (iconEl) {
							iconEl.removeClass(dropNode.attributes.iconCls);
							dropNode.attributes.iconCls = newCls;
							iconEl.addClass(newCls);
						}
					}
				}
			})
}